//
//  XGDataManager.m
//  BSBuDeJie
//
//  Created by monkey on 2018/12/11.
//  Copyright © 2018 itcast. All rights reserved.
//

#import <MJExtension/MJExtension.h>

#import "XGDataManager.h"
#import "XGNetworkTools.h"

#import "XGAdvertisementModel.h"
#import "XGNewsRecommendTagModel.h"
#import "XGSquareModel.h"
#import "XGTopicModel.h"
#import "XGCategoryModel.h"
#import "XGCategoryRecommendTagModel.h"

@implementation XGDataManager

#pragma mark - 请求数据

// 加载广告
+ (void)loadAdvertisement:(void (^)(XGAdvertisementModel * _Nullable, NSError * _Nullable))completion
{
    /*
     https://rich.kuwo.cn/AdService/kaiping/adinfo
     version = kwplayer_ip_9.1.3.0
     source = kwplayer_ip_9.1.3.0_TJ.ipa
     width = 750
     height = 1334
     appuid = 131572625
     */
    
    NSDictionary *parameters = @{@"version" : @"kwplayer_ip_9.1.3.0",
                                 @"source" : @"kwplayer_ip_9.1.3.0_TJ.ipa",
                                 @"width" : @(SCREEN_WIDTH),
                                 @"height" : @(SCREEN_HEIGHT),
                                 @"appuid" :@"131572625"
                                 };
    [XGNetworkTools GetRequest:kKuWoADAPIString parameters:parameters completionHandle:^(id  _Nullable responseObject, NSError * _Nullable error) {
        if (error) {
            completion(nil,error);
            return;
        }
        
        completion([XGAdvertisementModel mj_objectWithKeyValues:responseObject],nil);
    }];
}

+ (void)loadRecommendList:(void (^)(NSArray<XGNewsRecommendTagModel *> * _Nullable, NSError * _Nullable))completion
{
    /*
     https://api.budejie.com/api/api_open.php
     a = tag_recommend
     c = topic
     action = sub
     */
    
    NSDictionary *parameters = @{@"a" : @"tag_recommend",
                                 @"c" : @"topic",
                                 @"action": @"sub"
                                 };
    https://api.budejie.com/api/api_open.php/?a=tag_recommend&action=sub&c=topic
    [XGNetworkTools GetRequest:kBuDeJieAPIString parameters:parameters completionHandle:^(id  _Nullable responseObject, NSError * _Nullable error) {
        if (error) {
            completion(nil,error);
            return;
        }
        
        NSArray<XGNewsRecommendTagModel *> *dataArray = [[XGNewsRecommendTagModel mj_objectArrayWithKeyValuesArray:responseObject] copy];
        completion(dataArray,nil);
    }];
}

+ (void)loadSquareList:(void (^)(NSArray<XGSquareModel *> * _Nullable, NSError * _Nullable))completion
{
    /*
     https://api.budejie.com/api/api_open.php
     a = square
     c = topic
     */
    NSDictionary *parameters = @{@"a" : @"square",
                                 @"c" : @"topic",
                                 };
    [XGNetworkTools GetRequest:kBuDeJieAPIString parameters:parameters completionHandle:^(id  _Nullable responseObject, NSError * _Nullable error) {
        if (error) {
            completion(nil,error);
            return;
        }
        
        NSMutableArray<XGSquareModel *> *dataArrayM = [XGSquareModel mj_objectArrayWithKeyValuesArray:responseObject[@"square_list"]];
        [dataArrayM removeLastObject];
        completion([dataArrayM copy],nil);
    }];
}

+ (void)loadTopicWithType:(XGTopicType)type maxTime:(NSInteger)maxTime minTime:(NSInteger)minTime completion:(void (^)(NSArray<XGTopicModel *> * _Nullable, NSError * _Nullable))completion
{
    /*
     http://api.budejie.com/api/api_open.php
     a = list
     c = data
     type = 1为全部，10为图片，29为段子，31为音频，41为视频
     maxtime = 第一次加载帖子时候不需要传入此关键字，当需要加载下一页时：需要传入加载上一页时返回值字段“maxtime”中的内容
     */
    NSMutableDictionary *parameters = [NSMutableDictionary dictionaryWithDictionary:@{@"a" : @"list",
                                   @"c" : @"data",
                                   @"type" : @(type)}];
    if (maxTime != 0) {
        [parameters setObject:@(maxTime) forKey:@"maxtime"];
    }
    
    [XGNetworkTools GetRequest:kBuDeJieAPIString parameters:parameters completionHandle:^(id  _Nullable responseObject, NSError * _Nullable error) {
        if (error) {
            completion(nil,error);
            return;
        }
        
        // 字典转模型
        NSMutableArray<XGTopicModel *> *dataArrayM = [XGTopicModel mj_objectArrayWithKeyValuesArray:responseObject[@"list"]];
        
        if (minTime != 0) { // 加载最新数据时 过滤掉重复的数据
            NSMutableIndexSet *setM = [NSMutableIndexSet indexSet];
            [dataArrayM enumerateObjectsUsingBlock:^(XGTopicModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                if (obj.timeId <= minTime) {
                    [setM addIndex:idx];
                }
            }];
            
            [dataArrayM removeObjectsAtIndexes:setM];
        }
        
        completion([dataArrayM copy],nil);
    }];
}

+ (void)loadCategoryList:(void (^)(NSArray<XGCategoryModel *> * _Nullable, NSError * _Nullable))completion
{
    /*
     https://api.budejie.com/api/api_open.php
     a = category
     c = subscribe
     */
    
    NSDictionary *parameters = @{@"a" : @"category",
                                 @"c" : @"subscribe",
                                 };
    [XGNetworkTools GetRequest:kBuDeJieAPIString parameters:parameters completionHandle:^(id  _Nullable responseObject, NSError * _Nullable error) {
        if (error) {
            completion(nil,error);
            return;
        }
        
        NSMutableArray<XGCategoryModel *> *dataArrayM = [XGCategoryModel mj_objectArrayWithKeyValuesArray:responseObject[@"list"]];
        completion([dataArrayM copy],nil);
    }];
}

+ (void)loadCategoryTagListWithID:(NSInteger)ID page:(NSInteger)page completion:(void (^)(NSArray<XGCategoryRecommendTagModel *> *_Nullable dataArray,NSInteger total,NSError *_Nullable error))completion;
{
    /*
     http://api.budejie.com/api/api_open.php
     a = list
     c = subscribe
     category_id = 5
     page = 1
     */
    NSDictionary *parameters = @{@"a" : @"list",
                                 @"c" : @"subscribe",
                                 @"category_id" : @(ID),
                                 @"page" : @(page),
                                       };
    [XGNetworkTools GetRequest:kBuDeJieAPIString parameters:parameters completionHandle:^(id  _Nullable responseObject, NSError * _Nullable error) {
        if (error) {
            completion(nil,0,error);
            return;
        }
        
        NSMutableArray<XGCategoryRecommendTagModel *> *dataArrayM = [XGCategoryRecommendTagModel mj_objectArrayWithKeyValuesArray:responseObject[@"list"]];
        NSInteger total = [responseObject[@"total"] integerValue];
        completion([dataArrayM copy],total,nil);
    }];
}

+ (void)cancelRequest
{
    [XGNetworkTools cancelAllRequest];
}

@end
